package cn.wk.controller;

import cn.wk.constant.CourseTypeConstant;
import cn.wk.domain.CourseDoc;
import cn.wk.dto.CourseDto;
import cn.wk.exception.SystemError;
import cn.wk.query.CourseDocQuery;
import cn.wk.query.CourseQuery;
import cn.wk.service.ICourseService;
import cn.wk.domain.Course;
import cn.wk.util.AjaxResult;
import cn.wk.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    public ICourseService courseService;


    /**
     * 保存和修改公用的
     *
     * @param course 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Course course) {
        try {
            if (course.getId() != null)
                courseService.updateById(course);
            else
                courseService.insert(course);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    @PutMapping("/save")
    public AjaxResult saveCourse(@RequestBody @Valid CourseDto dto){
        System.out.println(dto);
        if(dto.getCourse().getId() != null){
            courseService.updateCourse(dto);
        }else {
            courseService.add(dto);
        }
        return AjaxResult.me().setSuccess(true).setMessage("保存成功");
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            courseService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Course get(@PathVariable("id") Long id) {
        return courseService.selectById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping("/pagelist")
    @PreAuthorize("hasAuthority('course:pagelist')")
    public List<Course> list() {

        return courseService.selectList(null);
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    @PreAuthorize("hasAuthority('course:list')")
    public PageList<Course> json(@RequestBody CourseQuery query) {
        return courseService.findList(query);
    }

    @PostMapping("/onLineCourse/{id}")
    @PreAuthorize("hasAnyAuthority('course:onLineCourse')")
    public AjaxResult onLineCourse(@PathVariable("id") Long id){

        return courseService.onLineOrOffline(id,true);
    }

    @PostMapping("/offLineCourse/{id}")
    public AjaxResult offLineCourse(@PathVariable("id") Long id){
        return courseService.onLineOrOffline(id,false);
    }

    //搜索
    @PostMapping("/queryCourses")
    public AjaxResult queryCourses(@RequestBody CourseDocQuery courseDocQuery){
        PageList<CourseDoc> courses = courseService.queryCourse(courseDocQuery);
        return AjaxResult.me().setResultObj(courses);
    }
}
